const express = require('express')
const dbConn = require('./db.js')
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }))

//允许ajax跨域
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/getdata', function(req, res, next) {
    let connection = dbConn();
    let sql = "SELECT * FROM user WHERE id=1"
    connection.connect();

    connection.query(sql, function(error, results, fields) {
        if (error) throw error;
        res.json(results);
    });

    connection.end();
})

app.get('/', function(req, res, next) {
    res.send('hello world')
})

app.listen(3000, () => console.log('服务器开始运行...'))